package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Food;
import com.itheima.pojo.Sports;
import com.itheima.service.FoodService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/food")
public class FoodController {
    @Reference
    private FoodService foodService;

    @RequestMapping("/upload") //文件上传
    public Result upload(MultipartFile excelFile){
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            if (list.size() != 0 && list != null) {
                List<Food> foodList = new ArrayList<>();
                for (String[] strings : list) {
                    Food food = new Food(strings[0], strings[1], Integer.parseInt(strings[2].trim()), strings[3], strings[4], Integer.parseInt(strings[5].trim()));
                    foodList.add(food);
                }
                foodService.addList(foodList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_FOOD_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_FOOD_SUCCESS);
    }

    @RequestMapping("/findPage") //分页查询
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return foodService.findPage(queryPageBean);
    }

    @RequestMapping("/add") //单项添加
    public Result add(@RequestBody Food food){
        try {
            foodService.add(food);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_KNOWLEDGE_FAIL);
        }
        return new Result(true,MessageConstant.ADD_KNOWLEDGE_SUCCESS);
    }

    @RequestMapping("/delete") //删除
    public Result delete(Integer id){
        try {
            foodService.delete(id);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_KNOWLEDGE_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_KNOWLEDGE_SUCCESS);
    }

    @RequestMapping("/edit") //修改
    public Result edit(@RequestBody Food food){
        try {
            foodService.edit(food);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_KNOWLEDGE_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_KNOWLEDGE_SUCCESS);
    }

    @RequestMapping("/getFoodTypes")//获得食物种类数据
    public Result getFoodTypes(){
        List<Map> foodTypes = null;
        try {
            foodTypes = foodService.getFoodTypes();
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_KNOWLEDGE_FAIL);
        }
        return new Result(true,MessageConstant.GET_KNOWLEDGE_SUCCESS,foodTypes);
    }
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Food> list =  foodService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
